import sys
import os
from google.appengine.ext.webapp import template

from google.appengine.api import users
from google.appengine.ext import webapp
from google.appengine.ext import db
from google.appengine.ext.webapp.util import run_wsgi_app
from google.appengine.dist import use_library
use_library('django', '0.96')
from os import environ
import models
import datetime
global ids
class MainPage(webapp.RequestHandler,db.Model):
	global ids
	def verifyLogin(self):
		user = users.get_current_user()
		if user:
			usercheck=models.userDetails.all()
			usercheck.filter("email =",user.email())
			try:
				currentuser=usercheck.fetch(1)[0]
				userdet=db.GqlQuery("SELECT * FROM userDetails WHERE email='"+user.email()+"'")
				for i in userdet:
					if i.privilege=='user':
						return
					elif i.approved==False:
						self.redirect("/usererror")
						return
				self.redirect("/admin")
			except:
				path = os.path.join(os.path.dirname(__file__),'form.html')#use template
				self.response.out.write(template.render(path,{"email":user.email()}))#using template
		else:
			self.redirect(users.create_login_url(self.request.uri))

	def get(self):
		self.verifyLogin()
		global ids
		ids=self.request.get("ids")
		category=[]		
		c1=db.GqlQuery("SELECT * FROM projectFields")
     		for i in c1:
			temp=[]
			temp.append(i.fieldname.replace(" ","_"))
			temp.append(i.fieldname)
			category.append(temp)
		lang=[]		
		c1=db.GqlQuery("SELECT * FROM languages")
     		for i in c1:
			lang.append(i.language)
		
		path = os.path.join(os.path.dirname(__file__),'create.html')#use template
		self.response.out.write(template.render(path,{"category":category,"language":lang}))#using template

	def post(self):
		global ids
		pro=db.GqlQuery("SELECT * FROM projectDetails")
		for k in pro:
			if k.projectid==ids:
				break
		c1=db.GqlQuery("SELECT * FROM languages")
		k.languages=""
		for i in c1:
			if self.request.get(i.language)=="yes":
				if k.languages=="":
					k.languages=k.languages+i.languageid
				else:
					k.languages=k.languages+","+i.languageid
		c1=db.GqlQuery("SELECT * FROM projectFields")
		k.fieldnames=""
     		for i in c1:
     			if self.request.get(i.fieldname.replace(" ","_"))=="yes":
				if(k.fieldnames==""):
					k.fieldnames=k.fieldnames+i.fieldid
				else:
	     				k.fieldnames=k.fieldnames+","+i.fieldid
		k.projectname=self.request.get("projectname")
		k.abstract=self.request.get("abstrct")
		k.link=self.request.get("link")
		k.confirmed=False
		k.put()
		c1=db.GqlQuery("SELECT * FROM teamDetails")
		for i in c1:
			if i.teamid==k.teamid:
				team=i.teammember.split(",")
				c2=db.GqlQuery("SELECT * FROM userDetails")
				for j in c2:
					if j.admissionnumber in team:
						if j.projectids==None:
							j.projectids=ids
						else:
							j.projectids=j.projectids+','+ids
						j.put()
		self.redirect("/user")

application = webapp.WSGIApplication([('/create', MainPage)],debug=True)

def main():
    run_wsgi_app(application)

if __name__ == "__main__":
    main()

